<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Layout extends Admin_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );

	public $model = "layout";
	public $view = "layout";
	public $permissionCreate = 'Layout.Create';
    public $permissionDelete = 'Layout.Delete';
    public $permissionEdit   = 'Layout.Edit';
    public $permissionView   = 'Layout.View';
	
    function __construct() {
        parent::__construct();
        $this->load->model($this->model.'_m',$this->model);
        $this->load->model('modules_m','modules');
        $this->load->model('Modules_position_m','mposition');
		$this->_model = $this->{$this->model};
    }
	function index($layout_id = '')
	{
		$layout = $this->_model->get($layout_id);
		if(!$layout)
		{
			show_404();
		}
		$this->current_permission = 'Layout.'.ucfirst($layout->file_name).'.Edit';
		$this->permissionEdit = $this->current_permission;
		$this->check_permission();

		$layout_name = str_replace('.php','',$layout->file_name);
		$this->data['layout_name'] = $layout_name;
		$this->data['layout_id'] = $layout_id;
		$this->data['layout'] =  $this->load->view($this->template->prefix_frontend.'layout/' . $layout_name , array('is_admin' => TRUE, ), TRUE);
		$this->data['position'] =  $this->mposition->select('m.module_id, module_name, module_title,mpos_id, layout_id,mpos_title,mpos_position,mpos_order')->gets_widget('',$layout->layout_id);
		$this->data['modules'] = $this->modules->get_many_by();
		$this->theme_data["main"] = $this->view."/index";
		$this->template->viewAdmin($this->theme_data, $this->data);
	}
	
	function edit($edit_id = 0)
	{
		$this->load->helper('file');
		$this->data['module_files'] = array();
		$module_files = get_dir_file_info(APPPATH.'views/'.$this->template->prefix_frontend.'/layout');
		if($module_files)
		{
			foreach($module_files as $file_name =>$file_info)
			{
				$file_namekey = str_replace('.php','',$file_name);
				$this->data['module_files'][$file_namekey] = $file_name;
			}
		}
		parent::edit($edit_id);
	}
	
	public function update()
	{
		$layout_name = $this->input->post("layout");
		$module = $this->input->post("moduleid");
		if(!$layout_name) die();
		if(!$module || count($module) <=0) die();
		$arr_update = array();
		for($i=0; $i<count($module); $i++){
			$arr_update[] = array(
				'mpos_id'=>$module[$i],
				'mpos_order'=>$i,
			);
		}

		$this->db->update_batch('modules_positions', $arr_update, 'mpos_id'); 
	}
	
	public function load_widget_config($id) {
		$id = (int) $id;
		$mwidget = $this->mposition->get($id);
		if($mwidget) {
			$module = $this->modules->get($mwidget->module_id);
			$mod_name = $module->module_name;
			$this->load->model(Modules::get_dir().$mod_name);
			$widget = array();
			//$widget['widget_config'] =  $widget;
			$widget['widget_config'] = array();
			$widget['widget_config']['title'] = $mwidget->mpos_title;
			$widget['widget_config']['config'] = json_decode($mwidget->mpos_config,true);
			$widget['widget_config']['layout_id'] = @$mwidget->layout_id;
			$widget['mod_name']=  $mod_name;
			$widget['widget_id']=  $id;
			$widget['widget_title']=  $mwidget->mpos_title;
			
			$this->load->view('admin/layout/update_widget',$widget);
			 
		}else {
			die('Something wrong!');
		}
		
	}
	
	public function update_widget() {
		$data = $this->input->post();
		if (!$data ) 
			die("data not found");
		
		$module_id = $data['mpos_id'];
		$module = $this->mposition->get_module($module_id);
		if (!$module) 
			die("module not found");
		$module_name = $module->module_name;
		$this->load->model(Modules::get_dir().$module_name);
		$data = $this->$module_name->module_update($data);
		$arr_default = array('mpos_title'=>'','mpos_id'=>0);
		$data = array_merge($arr_default,$data);
		
		$title = $data['mpos_title'];
		$id = $module_id;
		unset($data['mpos_id']);
		unset($data['mpos_title']);
		$data_widget = array();
		$data_widget['mpos_title'] = $title;
		$config = json_encode($data);
		$data_widget['mpos_config'] = $config;
		$this->mposition->update($module_id, $data_widget);
		
	}
	
	public function remove_widget() {
		if (!$this->input->is_ajax_request()) {
			die;
		}
		$wid = (int)$this->input->post('wid');
		if ($wid) {
			echo "delete $wid";
			$this->mposition->delete($wid);
		}
		die;
	}
	function add_widget(){
		if (!$this->input->is_ajax_request()) {
			die;
		}
		$module_id = $this->input->post("module_id");
		$position = $this->input->post("position");
		$layout = $this->input->post("layout");
		$module = $this->modules->get($module_id);
		if($module){
			$max_order = $this->db->select_max('mpos_order','max_order')
									->where('layout_id',$layout)
									->where('mpos_position',$position)
									->get('modules_positions')->first_row();
			$max_order = ($max_order->max_order >0)?$max_order->max_order:0;
			$arr_insert = array();
			$arr_insert['mpos_position'] = $position;
			$arr_insert['layout_id'] = $layout;
			$arr_insert['module_id'] =$module->module_id;
			$arr_insert['mpos_title'] =$module->module_title;
			$arr_insert['mpos_config'] =$module->module_config;
			$arr_insert['mpos_order'] = $max_order+1;
			$mod_pos_id = $this->mposition->insert($arr_insert);
			die('<li data-id="'.$mod_pos_id.'" id="moduleid_'.$mod_pos_id.'"><div class="w-control" data-id="' . $mod_pos_id. '">'
			. '<a href="#editWidget"  data-toggle="modal"  class="w-edit fa fa-wrench"></a>'
			. '<a href="#AlertRemove" data-toggle="modal" class="w-remove fa fa-trash-o"></a></div><span>'.$module->module_title.' : '.$arr_insert['mpos_title'].'</span></li>');
		}
		die("0");
	}
}
